home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gigarom 1
/
Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso
/
FILES
/
BBS
/
MUBBS
/
MUBBS etc.cpt
/
Module Source
/
Files Module
/
Xmodem Module Main.c
< prev
next >
Wrap
Text File
|
1991-11-21
|
16KB
|
508 lines
/*
* Xmodem module main.c
*
* This program source code and it's compiled version is
* Copyright (c) 1991 N. Hawthorn.
* This program source code and it's compiled version IS NOT IN THE
* PUBLIC DOMAIN ! Please read the "COPYRIGHT NOTICE / NH" file for details
* regarding use of this program source code and it's compiled version.
*
* This module's name is "Xmodem1", it's type is "MOD1", use a resource mover
* to assign a new number to it, that's why we name our modules !
*
* This is where it all starts...
*
*/
#define INMAIN
#include <SetUpA4.h>
#include "MUBBS Module.h"
#include <time.h>
struct PPP {
char filename[256]; /* the whole file and folder name */
char justname[64]; /* just the file name (for the user to see) */
long size; /* the size of a file either received or sent */
int mode; /* the mode send, receive */
int result; /* the result of the transfer */
char reserved[32]; /* some extra bytes for future use */
};
#define DOTS 60 /* SECTOR COUNTING DOTS PER LINE */
#define SECSIZ 0x80
#define BUFSIZE 0x90 /* input data buffer 128+ bytes */
#define ERRORMAX 20 /* MAX ERRORS BEFORE ABORT */
#define RETRYMAX 12 /* MAXIMUM RETRYS BEFORE ABORT */
#define RECEIVE 0 /* Receive a file */
#define SEND 1 /* Send a file */
#define ABORT 1 /* Return ERROR */
#define NOTFOUND 2 /* Return ERROR */
#define NOCREATE 3 /* Return ERROR */
#define SOH 1 /* START OF SECTOR CHAR */
#define EOT 4 /* end of transmission char */
#define ACK 6 /* acknowledge sector transmission */
#define NAK 21 /* error in transmission detected */
#define CAN 24 /* error in transmission detected */
#define LTRC 0x43 /* ASCII letter C */
static unsigned short Table[] = {
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};
pascal void main (mode1,G1,P) /* called from the main routines, and what mode to be in */
int mode1;
struct GS *G1; /* we point to the "global" struct in the Main Module here */
Ptr P;
{
Handle temph;
float version = 0.5; /* what version of MUBBS you are compatable with IE: .5 and above */
RememberA0(); SetUpA4(); /* This sets up the A4 register to access our globals */
asm { _RecoverHandle }; asm {move.l a0,temph}; HLock(temph); /* locks our module, do this ! */
G=G1; /* This MUST be the first thing you do in main only, it sets up our globals */
mode[u]=mode1; /* set up our mode so that you can read it anywhere */
switch (mode[u]) { /* any un-handled modes return error from this module */
case 3:
doit(P);
G->moduleresult=0;
break;
case 98:
versionck(version); /* just return after this call, don't modify anything */
break;
case 0:
strcpy (G->programmer,"N Hawthorn"); /* show the programmer's name up to 20 chars*/
G->moduleresult=0; /* this was also a init call if we need close call put 99 here */
break;
default:
G->moduleresult=1; /* return bad code */
};
HUnlock(temph); /* unlocks this module, do this ! */
RestoreA4(); /* call this when you are all done */
}
in1() /* special Xmodem input routine that waits just 2 seconds for timeout */
{
int n = 0;
unsigned long
secs1;
secs1=clock(); /* reset clock */
loop:
if (G->serin()) return(TRUE); /* no chars avail? */
if ((clock() - secs1) / CLOCKS_PER_SEC < 2) goto loop;
return FALSE;
}
/*
CRC Calculation
To calculate the 16 bit CRC the message bits are considered to be the
coefficients of a polynomial. This message polynomial is first
multiplied by X^16 and then divided by the generator polynomial
(X^16 + X^12 + X^5 + 1) using modulo two arithmetic. The remainder left
after the division is the desired CRC. Since a message block in the
Modem Protocol is 128 bytes or 1024 bits, the message polynomial will
be of order X^1023. The hi order bit of the first byte of the message
block is the coefficient of X^1023 in the message polynomial. The lo
order bit of the last byte of the message block is the coefficient of
X^0 in the message polynomial.
This function calculates the CRC used by the XMODEM/CRC Protocol.
The first argument is a pointer to the message block.
The second argument is the number of bytes in the message block.
The function returns an integer which contains the CRC.
*/
unsigned int updatecrc(b, crc)
unsigned char b;
unsigned short crc;
{
return Table[((crc >> 8) ^ b) & 0xFF] ^ (crc << 8);
}
doit(P)
struct PPP *P;
{
if (P->mode==RECEIVE) filereceive(P);
if (P->mode==SEND) filesend(P);
}
filereceive(P)
struct PPP *P;
{
int
i,
first,
errors,
cancnt,
toutcnt,
errorflag,
monitortemp;
char
condition;
unsigned char
bufr[BUFSIZE],
sectnum,
sectcurr,
sectcomp,
checksum;
unsigned int
j;
unsigned long
tempsize,
secs1;
FILE *stream;
monitortemp=G->monitor[u]; /* save the old monitor setting */
G->monitor[u]=FALSE; /* turn it off for now */
G->nottransfer[u]=FALSE; /* we are now in a transfer mode (I/O is full binary and fast) */
if ((stream = fopen(P->filename, "wb")) == NULL) {
print("FILE ERROR - cannot open/create %s\n",P->filename);
P->result=NOCREATE;
goto byebye1; /* byebye1 DOESN'T try to close the file */
}
else print("Xmodem Upload of \"%s\" from user\"%s\"\n",P->filename,G->username[u]);
send ("]]Ready to receive \"%s\"]",P->justname);
send ("Start your Xmodem (checksum) UPLOAD (send) now]");
send ("]Because this is CHECKSUM (not CRC) Xmodem, it may take a second to get going.]");
send ("(Press control - X several times to cancel)...]");
sectnum = errors = cancnt = tempsize = 0;
G->serflush();
G->serout(NAK); /* send first NAK incase they're waiting*/
condition=NAK; /* set the starting condition */
first=1; /* first record flag for EOT */
G->input[u]=0;
while(TRUE){
while ((G->input[u] != EOT) && (errors != ERRORMAX))
{
secs1 = clock(); /* Get clock ticks */
toutcnt=0;
errorflag = FALSE;
if (((sectnum) % DOTS) == 0) if (G->debuglevel > 0) print("UL<%3d>L%d ",sectnum,(u+1));
do /* get sync char */
{
if ( ! G->serin()) { /* no chars avail */
if ((clock() - secs1) > 400) { /* time to send another NAK/ACK ? */
secs1=clock(); /* reset clock */
if (toutcnt++ > 8) {
P->result=ABORT;
goto byebye; /* too many NAK/ACK sent where are you ? */
}
G->serout(condition);
};
}
else {
if (G->debuglevel > 1) print("\nWaiting for SOH Got char '%x'\n", G->input[u]);
if (G->input[u] == CAN) if (cancnt++ > 3) {
P->result=ABORT;
goto byebye;
}
if (G->input[u] != SOH && G->input[u] != EOT) G->serflush();
}
} while ((G->input[u] != SOH) && (G->input[u] != EOT));
if (G->input[u] == SOH) {
if (!in1()) {errorflag=TRUE; goto errout;} /* get sector count byte */
sectcurr = G->input[u];
if (!in1()) {errorflag=TRUE; goto errout;} /* get sector count byte */
sectcomp = G->input[u];
if (G->debuglevel > 1) print("Sector <$%x>, ~<$%x>, added= <$%x>", sectcurr, sectcomp,(sectcurr + sectcomp));
if (G->debuglevel > 1) print(", should be <$%x>\n", sectnum+1);
if (((sectcurr + sectcomp) & 0xFF) == 0xFF) { /* is the sector number received ok */
if (((sectnum + 1) & 0xFF) == sectcurr) { /* is it the correct sector number ? */
checksum = 0;
for (j = 0;j < SECSIZ;j++) { /* get the record put in file */
if (!in1()) {errorflag=TRUE; goto errout;} /* get data byte */
bufr[j]=G->input[u];
checksum += G->input[u];
/* print("\ndata <$%x>, cksm <$%x>",G->input[u] , checksum); */
};
if (!in1()) {errorflag=TRUE; goto errout;} /* get checksum byte */
if (checksum == G->input[u]) { /* get the checksum byte and check it */
for (j = 0;j < SECSIZ;j++) { /* get the record put in file */
if ((fputc(bufr[j],stream)) == EOF) {errorflag=TRUE; goto errout;}
};
errors = 0;
sectnum++; /* move up to the next sector number */
tempsize += SECSIZ;
if (G->debuglevel > 1) print("Good sector. sending <ACK>\n");
first=0; /* we got past first sector */
G->serflush();
condition=ACK; /* set for a time out */
G->serout(ACK);
}
else { /* its a bad checksum */
errorflag = TRUE;
print("checksum error, got <$%x> expected ",G->input[u]);
print("<$%x>\n",checksum);
};
}
else { /* not the correct sector number */
if (sectcurr == sectnum) { /* could it be the same one again ? */
if (G->debuglevel > 0) print("\nreceived duplicate sector %d\n",sectnum);
if (G->debuglevel > 0) print("Duplicate sector. sending <ACK>\n");
G->serflush();
G->serout(ACK);
}
else { /* wow, we're way out of sync ! */
if (G->debuglevel > 0 )print("Xmodem synch error\n");
errorflag = TRUE;
};
};
}
else { /* the sector number is corrupted */
if (G->debuglevel > 0) print("corrupted sector number received\n");
errorflag = TRUE;
};
}; /* "if G->input[u]==SOH" closed here */
errout:
if (errorflag == TRUE) { /* any errors ? */
errors++;
if (G->debuglevel > 0 ) {
print("C> Xmodem errors= %d\n",errors);
print("C> FILE ERROR or may be some other type of error. sending <NAK>\n");
}
wait(12); /* wait for 12 secs */
G->serflush();
condition=NAK; /* set for a time out */
G->serout(NAK);
};
}; /* end while loop, go back and wait for another record */
if ((G->input[u] == EOT) && (errors < ERRORMAX) && (first == 0)) { /* did we get a EOT not a SOH ? */
wait(2);
G->serout(ACK);
fclose(stream);
G->monitor[u]=monitortemp; /* return the monitor to original value */
G->nottransfer[u]=TRUE; /* back to normal mode for I/O */
P->size=tempsize;
if (G->debuglevel > 0) print("\n");
P->result=0; /* everything is OK here ! */
return;
};
wait(12); /* wait for 12 secs */
G->serflush();
}; /* closes the WHILE(TRUE) loop, try again */
byebye: /* will go here if cancel received */
fclose(stream);
byebye1:
P->size=0;
G->monitor[u]=monitortemp; /* return the monitor to original value */
G->nottransfer[u]=TRUE; /* back to normal mode for I/O */
if (G->debuglevel > 0) print("\n");
}
filesend(P)
struct PPP *P;
{
int
i,
endflg,
cancnt,
attempts,
crcflg,
monitortemp;
unsigned char
bufr[BUFSIZE],
sectnum,
checksum;
unsigned int
j,crc;
unsigned long
tempsize;
FILE *stream;
monitortemp=G->monitor[u]; /* save the old monitor setting */
G->monitor[u]=FALSE; /* turn it off for now */
G->nottransfer[u]=FALSE; /* we are now in a transfer mode (I/O is full binary and fast) */
if ((stream = fopen(P->filename, "rb")) == NULL) {
print("FILE ERROR - cannot open %s\n",P->filename);
P->result=NOTFOUND;
goto byebye1;
}
else print("Xmodem Download \"%s\" by user \"%s\"\n",P->filename,G->username[u]);
send ("]]Ready to send \"%s\"]",P->justname);
send ("Start your Xmodem DOWNLOAD (receive) now]");
send ("(Press control - X several times to cancel)...]");
G->serflush();
attempts = 0;
sectnum = 1;
j = 0;
while ((G->input[u] != NAK && G->input[u] != LTRC) && (j++ < (ERRORMAX*2))) {
if (!G->in()) { /* get start byte */
P->result=ABORT;
goto byebye;
}
if (G->debuglevel > 1) print ("\nStart char was '%x'\n", G->input[u]);
if (G->input[u] == CAN) {
P->result=ABORT;
goto byebye;
}
if (j >= (ERRORMAX*2)) {
if (G->debuglevel > 1) print("Receiver not sending NAKs or Cs\n");
P->result=ABORT;
goto byebye;
}
}
if (G->debuglevel > 1) print ("Got start char: '%x'\n", G->input[u]);
crcflg=FALSE;
if (G->input[u] == LTRC) crcflg=TRUE; /* show we're in CRC mode */
G->serflush ();
cancnt=0;
endflg=FALSE;
tempsize = 0;
do {
attempts = 0;
for (j = 0; j < SECSIZ; j++) {
if ((i = fgetc(stream)) == EOF) {i=0; endflg=TRUE;}
bufr[j]=i;
}
do {
if (((sectnum - 1) % DOTS) == 0) if (G->debuglevel > 0) print("DL<%4d>L%d ",sectnum,(u+1));
G->serout(SOH);
G->serout(sectnum);
G->serout(~sectnum);
checksum = 0;
crc = 0;
for (j = 0; j < SECSIZ; j++) {
if (crcflg) { crc = updatecrc(bufr[j],crc); } /* for CRC */
else checksum += bufr[j]; /* for checksum mode */
G->serout(bufr[j]);
}
if (crcflg) {
if (G->debuglevel > 1) print("ENDING CRC is <$%x>\n", crc);
G->serout(crc >> 8); /* CRC high */
G->serout(crc); /* CRC low */
}
else G->serout(checksum);
tempsize += SECSIZ;
attempts++;
if (!G->in()) {
P->result=ABORT;
goto byebye; /* get byte */
}
if (G->input[u] == CAN){
if ((cancnt++) > 4) {
P->result=ABORT;
goto byebye;
}
}
if (G->debuglevel > 1) print ("Response char is '%x'\n", G->input[u]);
} while ((G->input[u] != ACK) && (attempts != RETRYMAX) && (!endflg));
sectnum++;
} while ((attempts != RETRYMAX) && (!endflg));
fclose(stream);
if (attempts == RETRYMAX)
{
if (G->debuglevel > 0) print("no acknowledgment of sector, aborting\n");
P->result=ABORT;
goto byebye;
}
else
{
attempts = 0;
do {
G->serout(EOT);
if (!G->in()) { /* get sector count byte */
P->result=ABORT;
goto byebye;
}
attempts++;
} while ((G->input[u] != ACK) && (attempts != RETRYMAX));
if (attempts == RETRYMAX) if (G->debuglevel > 1) print("no acknowledgment of end of file\n");
}
P->size=tempsize;
G->monitor[u]=monitortemp; /* return the monitor to original value */
G->nottransfer[u]=TRUE; /* back to normal mode for I/O */
if (G->debuglevel > 0) print("\n");
P->result=0; /* its all OK */
return;
byebye:
fclose(stream);
byebye1:
P->size=0;
G->monitor[u]=monitortemp; /* return the monitor to original value */
G->nottransfer[u]=TRUE; /* back to normal mode for I/O */
if (G->debuglevel > 0) print("\n");
}